/**
 * @author Caius
 * @description 定义股票信息监听
 * @version 1.0
 * @since Created in 2025-01-17
 */

package com.itsense.stock.mq;

import com.github.benmanes.caffeine.cache.Cache;
import com.itsense.stock.service.StockService;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Slf4j
public class StockMQMsgListener {
    @Autowired
    private Cache<String, Object> caffeineCache;
    @Autowired
    private StockService stockService;

    @RabbitListener(queues = "innerMarketQueue")
    public void refreshInnerMarketInfo(Date startDate){
        //返回获取数据的时间毫秒差
        long diffTime = DateTime.now().getMillis() - new DateTime(startDate).getMillis();
        if (diffTime > 60000l) {
            log.error("大盘发送消息时间: {},延时: {}",new DateTime(startDate).toString("yyyy-MM-dd HH:mm:ss"),diffTime);
        }
        //刷新缓存
        //删除旧数据
        caffeineCache.invalidate("innerMarketKey");
        //回调函数之后实现对应数据从数据库进行更新
        stockService.getInnerMarketInfo();
    }
}
